import collections


class Solution(object):
    def reconstructQueue(self, people):
        """
        :type people: List[List[int]]
        :rtype: List[List[int]]
        """
        n = len(people)
        out = [None] * n
        for i in range(n):
            for j in range(i + 1, n):
                if people[j][0] < people[i][0] or (people[j][0] == people[i][0] and people[j][1] < people[i][1]):
                    temp = people[j].copy()
                    people[j] = people[i]
                    people[i] = temp
        for item in people:
            index = 0
            count = 0
            while count <= item[1]:
                if not out[index]:
                    if count == item[1]:
                        out[index] = item
                        break
                    else:
                        count += 1
                else:
                    if out[index][0] >= item[0]:
                        count += 1
                index += 1
            out[index] = item
        return out


data = Solution()
people =  [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
print(data.reconstructQueue(people))
